"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 服务端工具
Case Name   : 备份结果不包含指定对象自身的信息
              (--exclude-self同-t数关联使用)
Create At   : 2021/1/13
Owner       : opentestcase002
Description :
    1.连接数据库并创建数据库
    2.在创建好的数据库中创建表插入数据，创建视图
    3.备份结果不包含指定对象自身的信息(--exclude-self同-t数关联使用)
    4.清理环境
Expect      :
    1.数据库连接成功
    2.创建数据成功
    3.导出成功
    4.清理环境成功
History     :
    modify by opentestcase002 20220811:研发代码变更，优化断言
    modify by opentestcase002 20221130:研发代码变更，优化断言
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        text = f'-----{os.path.basename(__file__)} start-----'
        self.log.info(text)
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.dbuser_node = Node('dbuser')
        self.db_name = 'db_dump_case0081'
        self.t_name1 = 't_dump_case0081_01'
        self.t_name2 = 't_dump_case0081_02'
        self.v_name1 = 'v_dump_case0081_01'
        self.v_name2 = 'v_dump_case0081_02'
        self.constant = Constant()

    def test_server_tools(self):
        text = '-----step1:连接数据库并创建数据库,expect:成功-----'
        self.log.info(text)
        sql_cmd1 = f'''drop database if exists {self.db_name};
            create database {self.db_name};
                   '''
        msg1 = self.sh_primy.execut_db_sql(sql_cmd1)
        self.log.info(msg1)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, msg1,
                      '执行失败:' + text)

        text = '-----step2:在创建好的数据库中创建表插入数据，创建视图,expect:成功-----'
        self.log.info(text)
        sql_cmd2 = f''' 
                drop table if exists {self.t_name1};
                drop table if exists {self.t_name2};
                drop view if exists {self.v_name1};
                drop view if exists {self.v_name2};
                create table {self.t_name1} (i int,d int );
                insert into {self.t_name1} values(1,2),(2,3),(3,4);
                create table {self.t_name2} (id int,dd int);
                insert into {self.t_name2} values(11,22),(21,34),(31,45);
                create view {self.v_name1} as select * from {self.t_name1};
                create view {self.v_name2} as select * from {self.t_name2};
                    '''
        msg2 = self.sh_primy.execut_db_sql(sql_cmd2, dbname=self.db_name)
        self.log.info(msg2)
        self.assertEqual(2, msg2.count('CREATE TABLE'), '执行失败:' + text)
        self.assertEqual(2, msg2.count('INSERT'), '执行失败:' + text)
        self.assertEqual(2, msg2.count('CREATE VIEW'), '执行失败:' + text)

        text = '-----step3:备份结果不包含指定对象自身的信息(--exclude-self' \
               '同-t参数关联使用),expect:成功-----'
        self.log.info(text)
        excute_cmd3 = f'''source {macro.DB_ENV_PATH} ;
                gs_dump -p {self.dbuser_node.db_port} {self.db_name} -F p
                --exclude-self -t {self.t_name2}; '''
        self.log.info(excute_cmd3)
        msg3 = self.dbuser_node.sh(excute_cmd3).result()
        self.log.info(msg3)
        self.assertIn(self.constant.GS_DUMP_SUCCESS_MSG, msg3)

    def tearDown(self):
        text = '-----step4:清理环境,expect:成功-----'
        self.log.info(text)
        sql_cmd4 = f'''drop database if exists {self.db_name}; '''
        msg4 = self.sh_primy.execut_db_sql(sql_cmd4)
        self.log.info(msg4)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, msg4,
                      '执行失败:' + text)
        text = f'-----{os.path.basename(__file__)} end-----'
        self.log.info(text)
